Skip to content

perf: make walking ~10% faster#247

Merged
bartveneman merged 4 commits into
mainfrom
claude/optimize-walker-performance-a2Naj
May 19, 2026
Merged

perf: make walking ~10% faster#247
bartveneman merged 4 commits into
mainfrom
claude/optimize-walker-performance-a2Naj

Conversation

@bartveneman
Copy link
Copy Markdown
Member

Summary

Refactored the walk() and traverse() functions to directly access the CSS data arena instead of relying on node object properties. This change improves performance by avoiding repeated object property lookups and enables more efficient tree traversal.

Key Changes

  • Refactored walk() function: Now extracts arena, source, and index from the node and uses arena methods (get_type(), get_first_child(), get_next_sibling()) for traversal instead of accessing node properties
  • Refactored traverse() function: Similarly updated to use arena-based access for both enter and leave callbacks
  • Introduced internal helper functions: Added _walk() and _traverse() private functions that accept arena, source, and index parameters to avoid repeated extraction logic
  • Added internal accessors to CSSNode: Exposed __get_source() and __get_index() methods to allow walk/traverse functions to access the underlying data needed for arena operations
  • Updated .gitignore: Added package-lock.json to ignore list

Implementation Details

  • The refactoring maintains the same public API and control flow behavior (BREAK, SKIP)
  • Child traversal now uses arena indices (with 0 as null sentinel) instead of null-based linked list navigation
  • Depth calculation logic remains unchanged but is now applied consistently in both public and private functions
  • The helper functions reduce code duplication between the public entry points and recursive traversal logic

https://claude.ai/code/session_01UVBKCBKB11qjh9k2PwbLen

claude added 3 commits May 17, 2026 22:31
…cations

- Rewrite walk/traverse internals to navigate the arena directly using
  integer indices (get_first_child/get_next_sibling) instead of going
  through the first_child/next_sibling CSSNode getters for sibling
  traversal bookkeeping
- Eliminate the { enter, leave } object created on every recursive call
  in traverse — previously N extra allocations per traversal, now zero
- CSSNode wrappers are still created exactly once per visited node for
  callback invocations; the root node passed in is reused as-is
- Add __get_source() and __get_index() internal helpers to CSSNode to
  expose the arena index and source string needed by the new walker

https://claude.ai/code/session_01UVBKCBKB11qjh9k2PwbLen
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 17, 2026

Bundle Report

Changes will increase total bundle size by 1.48kB (0.78%) ⬆️. This is within the configured threshold ✅

Detailed changes
Bundle name Size Change
@projectwallace/css-parser-esm 190.88kB 1.48kB (0.78%) ⬆️

Affected Assets, Files, and Routes:

view changes for bundle: @projectwallace/css-parser-esm

Assets Changed:

Asset Name Size Change Total Size Change (%)
css-node-BTgoFWDU.js (New) 14.88kB 14.88kB 100.0% 🚀
walk.js 1.36kB 3.18kB 74.56% ⚠️
css-node-C3t4b8Jt.js (Deleted) -14.76kB 0 bytes -100.0% 🗑️

Files in css-node-BTgoFWDU.js:

  • ./src/css-node.ts → Total Size: 14.66kB

Files in walk.js:

  • ./src/walk.ts → Total Size: 3.03kB

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (main@85bc48d). Learn more about missing BASE report.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #247   +/-   ##
=======================================
  Coverage        ?   93.20%           
=======================================
  Files           ?       17           
  Lines           ?     3062           
  Branches        ?      853           
=======================================
  Hits            ?     2854           
  Misses          ?      208           
  Partials        ?        0           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bartveneman
Copy link
Copy Markdown
Member Author

before

── Table 1: Wallace CSS Parser ──────────────────────────────────────────

┌─────────┬─────────────┬───────────┬────────────────────┬─────────────────┬────────────────┬──────────────────────┐
│ (index) │ File        │ Size      │ Tokenize (ops/sec) │ Parse (ops/sec) │ Walk (ops/sec) │ Parse+Walk (ops/sec) │
├─────────┼─────────────┼───────────┼────────────────────┼─────────────────┼────────────────┼──────────────────────┤
│ 0       │ 'Large'     │ '3 KB'    │ '23451'            │ '18072'         │ '88543'        │ '14850'              │
│ 1       │ 'Bootstrap' │ '274 KB'  │ '303'              │ '228'           │ '1302'         │ '194'                │
│ 2       │ 'Tailwind'  │ '3557 KB' │ '24'               │ '17'            │ '91'           │ '14'                 │
└─────────┴─────────────┴───────────┴────────────────────┴─────────────────┴────────────────┴──────────────────────┘

── Table 2: Parse+Walk Memory – Wallace ─────────────────────────────────

┌─────────┬─────────────┬───────────┬───────────┐
│ (index) │ File        │ Size      │ Wallace   │
├─────────┼─────────────┼───────────┼───────────┤
│ 0       │ 'Large'     │ '3 KB'    │ '0.2 MB'  │
│ 1       │ 'Bootstrap' │ '274 KB'  │ '13.2 MB' │
│ 2       │ 'Tailwind'  │ '3557 KB' │ '94.5 MB' │
└─────────┴─────────────┴───────────┴───────────┘

@bartveneman
Copy link
Copy Markdown
Member Author

after:

── Table 1: Wallace CSS Parser ──────────────────────────────────────────

┌─────────┬─────────────┬───────────┬────────────────────┬─────────────────┬────────────────┬──────────────────────┐
│ (index) │ File        │ Size      │ Tokenize (ops/sec) │ Parse (ops/sec) │ Walk (ops/sec) │ Parse+Walk (ops/sec) │
├─────────┼─────────────┼───────────┼────────────────────┼─────────────────┼────────────────┼──────────────────────┤
│ 0       │ 'Large'     │ '3 KB'    │ '23423'            │ '18080'         │ '99633'        │ '15073'              │
│ 1       │ 'Bootstrap' │ '274 KB'  │ '304'              │ '227'           │ '1464'         │ '197'                │
│ 2       │ 'Tailwind'  │ '3557 KB' │ '24'               │ '17'            │ '100'          │ '15'                 │
└─────────┴─────────────┴───────────┴────────────────────┴─────────────────┴────────────────┴──────────────────────┘

── Table 2: Parse+Walk Memory – Wallace ─────────────────────────────────

┌─────────┬─────────────┬───────────┬───────────┐
│ (index) │ File        │ Size      │ Wallace   │
├─────────┼─────────────┼───────────┼───────────┤
│ 0       │ 'Large'     │ '3 KB'    │ '0.2 MB'  │
│ 1       │ 'Bootstrap' │ '274 KB'  │ '13.2 MB' │
│ 2       │ 'Tailwind'  │ '3557 KB' │ '94.5 MB' │
└─────────┴─────────────┴───────────┴───────────┘

@bartveneman bartveneman changed the title Refactor walk and traverse to use arena-based node access perf: make walking ~10% faster May 19, 2026
@bartveneman bartveneman merged commit a0f9343 into main May 19, 2026
5 checks passed
@bartveneman bartveneman deleted the claude/optimize-walker-performance-a2Naj branch May 19, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants